home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / pcw.zip / MOUSE.C < prev    next >
Text File  |  1990-01-16  |  11KB  |  300 lines

  1. /***********************************************************/
  2. /* File Id.                  Mouse.C                       */
  3. /* Author.                   Stan Milam.                   */
  4. /* Date Written.             11/27/88.                     */
  5. /* Date Last Modified.                                     */
  6. /*                                                         */
  7. /*           (c) Copyright 1989-90 by Stan Milam           */
  8. /*                                                         */
  9. /* Comments:  This file represents a group of mouse func-  */
  10. /* tions for use with a Microsoft Mouse or compatible.     */
  11. /***********************************************************/
  12.  
  13. #include <dos.h>
  14. #include <string.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include "pcwproto.h"
  18.  
  19. static void get_x_coord(void);
  20.  
  21. static union REGS regs;
  22. static int   m_hide_flag = 1;               /* Hidden set to true */
  23. static int   x_coord;
  24.  
  25. int  mbuttons;
  26. int  mpresent = 0;
  27.  
  28. /***********************************************************/
  29. /*                         Init_Mouse                      */
  30. /*                                                         */
  31. /* This function will initialize the Mouse if one is pre-  */
  32. /* sent.  It will return 1 if a mouse is present and NULL  */
  33. /* if one is not.  If a Mouse is installed the number of   */
  34. /* mouse buttons will be stored in a GLOBAL, mbuttons.     */
  35. /***********************************************************/
  36.  
  37. int init_mouse(void) {
  38.  
  39.     memset(®s, '\0', sizeof(union REGS));
  40.     regs.x.ax = 0;
  41.     int86(0x33,®s,®s);
  42.     if (regs.x.ax == 0) {
  43.        mbuttons = 0;
  44.        return(regs.x.ax);
  45.     }
  46.     else {
  47.        mbuttons = regs.x.bx;
  48.        if (mpresent) mpresent = 0;     /* Turn off the Mouse */
  49.        else mpresent = 1;              /* Tell the world we have a mouse */
  50.        get_mpressed(0);                /* Clear LEFT Mouse Button */
  51.        get_mpressed(1);                /* Clear RITE Mouse Button */
  52.        return(1);
  53.    }
  54. }
  55.  
  56. /***********************************************************/
  57. /*                         Show_Mouse                      */
  58. /*                                                         */
  59. /* This function will show the mouse.  It will first check */
  60. /* a STATIC variable to make sure it is already hidden     */
  61. /* before proceeding.                                      */
  62. /***********************************************************/
  63.  
  64. void show_mouse(void) {
  65.  
  66.      if (m_hide_flag) {
  67.         m_hide_flag = 0;
  68.         memset(®s, '\0', sizeof(union REGS));
  69.         regs.x.ax = 1;
  70.         int86(0x33, ®s, ®s);
  71.      }
  72. }
  73.  
  74. /***********************************************************/
  75. /*                         Hide_Mouse                      */
  76. /*                                                         */
  77. /* This function will hide the Mouse cursor if it is visi- */
  78. /* ble and reset the hide flag.                            */
  79. /***********************************************************/
  80.  
  81. void hide_mouse(void) {
  82.  
  83.      if (m_hide_flag == 0) {
  84.         m_hide_flag = 1;
  85.         memset(®s, '\0', sizeof(union REGS));
  86.         regs.x.ax = 2;
  87.         int86(0x33, ®s, ®s);
  88.      }
  89. }
  90.  
  91. /***********************************************************/
  92. /*                          Get_Mpos                       */
  93. /*                                                         */
  94. /* This function will take three pointers to integers as   */
  95. /* arguments.  They will return the row, column, and the   */
  96. /* button status of the Mouse.  This information is return-*/
  97. /* ed REAL TIME.  This means this info may not be what you */
  98. /* want.  For example, the mouse may be in motion when you */
  99. /* make this call, but you will not know the destination of*/
  100. /* the Mouse. Good for use in a tight loop.                */
  101. /* Button Status:  Bit 0 set if left button pressed.       */
  102. /*                 Bit 1 set if rite button pressed.       */
  103. /***********************************************************/
  104.  
  105. void get_mpos(int *row, int *col, int *bstatus)  {
  106.  
  107.      memset(®s, '\0', sizeof(union REGS));
  108.      get_x_coord();
  109.      regs.x.ax = 3;
  110.      int86(0x33, ®s, ®s);
  111.      *row      = (regs.x.dx / 8) + 1;
  112.      *col      = (regs.x.cx / x_coord) + 1;
  113.      *bstatus  = regs.x.bx;
  114. }
  115.  
  116. /***********************************************************/
  117. /*                          Set_Mpos                       */
  118. /*                                                         */
  119. /* This function will set the mouse position.              */
  120. /***********************************************************/
  121.  
  122. void set_mpos(int row, int col)  {
  123.  
  124.      memset(®s, '\0', sizeof(union REGS));
  125.      get_x_coord();
  126.      regs.x.ax = 4;
  127.      regs.x.dx = (row * 8) - 1;
  128.      regs.x.cx = (col * x_coord) - 1;
  129.      int86(0x33, ®s, ®s);
  130. }
  131.  
  132. /***********************************************************/
  133. /*                        Get_Mpressed                     */
  134. /*                                                         */
  135. /* This function will return button pressed info.          */
  136. /* Specifically, it will return the number of times a but- */
  137. /* ton has been pressed since the last call for that button*/
  138. /* Call with: 0 = left button.                             */
  139. /*            1 = rite button.                             */
  140. /*            2 = mid  button.  (Logitech Only)            */
  141. /***********************************************************/
  142.  
  143. int get_mpressed(int button) {
  144.  
  145.     memset(®s, '\0', sizeof(union REGS));
  146.     regs.x.ax = 5;
  147.     regs.x.bx = button;
  148.     int86(0x33, ®s, ®s);
  149.     return(regs.x.bx);
  150. }
  151.  
  152. /***********************************************************/
  153. /*                        Get_Mreleased                    */
  154. /*                                                         */
  155. /* This function works as above except that it returns the */
  156. /* number of times a button has been released.             */
  157. /***********************************************************/
  158.  
  159. int get_mreleased(int button) {
  160.  
  161.     memset(®s, '\0', sizeof(union REGS));
  162.     regs.x.ax = 6;
  163.     regs.x.bx = button;
  164.     int86(0x33, ®s, ®s);
  165.     return (regs.x.bx);
  166. }
  167.  
  168. /***********************************************************/
  169. /*                           Mframe                        */
  170. /*                                                         */
  171. /* This function incorporates two mouse driver functions:  */
  172. /* set horizontal & vertical boundries.  It will confine   */
  173. /* the Mouse cursor to rectangular block on the screen.    */
  174. /***********************************************************/
  175.  
  176. void mframe(int urow, int ucol, int lrow, int lcol) {
  177.  
  178. /* Set vertical boundries of Mouse */
  179.  
  180.      memset(®s, '\0', sizeof(union REGS));
  181.      regs.x.ax = 8;
  182.      regs.x.dx = (urow * 8) - 1;
  183.      regs.x.cx = (lrow * 8) - 1;
  184.      int86(0x33, ®s, ®s);
  185.  
  186. /* Set horizontal boundries of Mouse */
  187.  
  188.      get_x_coord();
  189.      regs.x.ax = 7;
  190.      regs.x.dx = (ucol * x_coord) - 1;
  191.      regs.x.cx = (lcol * x_coord) - 1;
  192.      int86(0x33, ®s, ®s);
  193. }
  194.  
  195. /***********************************************************/
  196. /*                         Set_Mtype                       */
  197. /*                                                         */
  198. /* This function will set the mouse cursor Type. You have  */
  199. /* two choices: The default Mouse cursor or taking over the*/
  200. /* hardware cursor.  If you take the default, there will be*/
  201. /* two cursors on the screen, the Mouse & the Hardware.    */
  202. /* If you choose the default you can specify which char-   */
  203. /* acter it will be and its color.                         */
  204. /*                                                         */
  205. /* Default Cursor:                                         */
  206. /*      ctype = 0;                                         */
  207. /*      arg1  = 0;                                         */
  208. /*      arg2  = (attr << 8) | char                         */
  209. /* Hardware Cursor:                                        */
  210. /*      ctype = 1;                                         */
  211. /*      arg1  = 0x77ff;                                    */
  212. /*      arg2  = 0x7700;                                    */
  213. /***********************************************************/
  214.  
  215. void set_mtype(int ctype, int arg1, int arg2) {
  216.  
  217.      memset(®s, '\0', sizeof(union REGS));
  218.      regs.x.ax = 10;
  219.      regs.x.bx = ctype;
  220.      regs.x.cx = arg1;
  221.      regs.x.dx = arg2;
  222.      int86(0x33, ®s, ®s);
  223. }
  224.  
  225. /**********************************************************/
  226. /*                   Save_Mouse_State                     */
  227. /*                                                        */
  228. /* This function can be used to save the state of the     */
  229. /* Mouse before executing another program that uses the   */
  230. /* Mouse.  On return from the child process a call to Re- */
  231. /* store_Mouse_State will, of course, restore your Mouse. */
  232. /**********************************************************/
  233.  
  234. char *save_mouse_state(void) {
  235.  
  236.    void far *buffer;
  237.    char *temp;
  238.    struct SREGS sregs;
  239.  
  240.    memset(®s, '\0', sizeof(union REGS));
  241.    regs.x.ax = 21;
  242.    int86(0x33,®s,®s);
  243.    temp = malloc(regs.x.bx);
  244.    buffer = (void far *) temp;
  245.    if (buffer == (void far *) 0) return(0);
  246.    regs.x.ax = 22;
  247.    regs.x.dx = FP_OFF(buffer);
  248.    sregs.es  = FP_SEG(buffer);
  249.    int86x(0x33,®s,®s,&sregs);
  250.    return(temp);
  251. }
  252.  
  253. /**********************************************************/
  254. /*                   Restore_Mouse_State                  */
  255. /*                                                        */
  256. /* Restores the Mouse state that was previously saved.    */
  257. /* Accepts a pointer to the saved buffer.                 */
  258. /**********************************************************/
  259.  
  260. void restore_mouse_state(char *buffer) {
  261.  
  262.    struct SREGS sregs;
  263.    void far *temp;
  264.  
  265.    memset (®s, 0, sizeof(union REGS));
  266.    temp = (void far *) buffer;
  267.    regs.x.ax = 23;
  268.    regs.x.dx = FP_OFF(temp);
  269.    sregs.es  = FP_SEG(temp);
  270.    int86x(0x33,®s,®s,&sregs);
  271.    free(buffer);
  272. }
  273.  
  274.  
  275. /**********************************************************/
  276. /*                       Get_X_Coord                      */
  277. /*                                                        */
  278. /* This function sets the variable x_coord to the proper  */
  279. /* value to determine the true column of the mouse. Is    */
  280. /* determined by the video mode.                          */
  281. /**********************************************************/
  282.  
  283. static void get_x_coord(void) {
  284.  
  285.    int cols, mode, apage;
  286.  
  287.    vgetmode(&cols, &mode, &apage);
  288.    switch(mode) {
  289.       case  0 :
  290.       case  1 : x_coord = 16; break;
  291.       case  2 :
  292.       case  3 :
  293.       case  7 : x_coord =  8; break;
  294.       case  4 :
  295.       case  5 : x_coord =  2; break;
  296.       case 13 : x_coord = 16; break;
  297.       default : x_coord =  1; break;
  298.    }
  299. }
  300.